package intention;

import java.awt.TrayIcon.MessageType;
import javax.swing.Timer;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;

public class IntentionLogic {

    private IntentionView app;
    private int[] warnings;
    ArrayList warnTimers;
    Timer endTimer;

    public IntentionLogic(IntentionView appView) {
        this.app = appView;
        app.startButton.addActionListener(beginListen);
        app.aField2.addKeyListener(enterListen);
        app.trayIcon.addMouseMotionListener(timeLeft);
        app.trayIcon.getPopupMenu().addActionListener(finishedListen);
        warnTimers = new ArrayList();
    }
    public KeyListener enterListen = new KeyListener() {

        public void keyTyped(KeyEvent e) {
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                pressBegin();
            }
        }

        public void keyReleased(KeyEvent e) {
        }
    };
    private ActionListener warnListen = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            Long now = new Date().getTime();
            Long passed = now - app.startTime;
            int left = app.getTime() - Integer.valueOf(passed.toString()); //time left in milliseconds
            int minutes = left / (1000 * 60) + 0;
            app.triggerWarning(minutes);
        }
    };
    public ActionListener beginListen = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            pressBegin();
        }
    };
    public ActionListener finishedListen = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            stopTimers();
            app.triggerFinished();
        }
    };
    private ActionListener endListen = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            stopTimers();
            app.triggerEnd();
        }
    };

    private void stopTimers() {
        for (Object warnTimer : warnTimers) {
            Timer timer = (Timer) warnTimer;
            timer.stop();
        }
        warnTimers = null;
        endTimer.stop();
        endTimer = null;
    }
    public MouseMotionListener timeLeft = new MouseMotionListener() {
//<<FIX>> Update to mouselistener and make it refresh every second.

        public void mouseMoved(MouseEvent e) {
            app.showTimeLeft();
        }

        public void mouseDragged(MouseEvent e) {
            app.showTimeLeft();
        }
    };

    //User presses Begin
    public void pressBegin() {
        app.startTime = new Date().getTime();
        int minute = 1000 * 60;
        int end = app.getTime();
        warnings = new int[3]; //Warnings, in minutes
        warnings[0] = 5;
        warnings[1] = 1;
        warnings[2] = 15;

        //Init end event
        endTimer = new Timer(end, endListen);
        endTimer.setRepeats(false);
        endTimer.start();
        //Init warning timers
        warnTimers = new ArrayList();
        for (int warn : warnings) {
            if (end > warn * minute) {
                Timer warnTimer = new Timer(end - warn * minute, warnListen);
                warnTimer.setRepeats(false);
                warnTimer.start();
                warnTimers.add(warnTimer);
            }
        }
        //Minimize App to Tray
        app.minToTray();

        //Indicate that the app is minimized
        app.trayIcon.displayMessage("Intention", "Intention will wait for you to " + app.getIntention() + " for " + (app.getTime() / (60 * 1000)) + " minutes. You can check the time remaining here, or select \"Finished\" when you are done.", MessageType.NONE);
    }
}
